use dta/referenda, clear

global abno "323 352 373 415 416 423 444 458 469 470 484 489_2 499 500 503 508 523 527 528 536 543 553 328 398 437 439 451 457 480 489_1 492 507 509 514 531 534 551"
global ab "p323 p352 p373 p415 p416 p423 p444 p458 p469 p470 p484 p489_2 p499 p500 p503 p508 p523 p527 p528 p536 p543 p553 p328 p398 p437 p439 p451 p457 p480 p489_1 p492 p507 p509 p514 p531 p534 p551"
global bab "bp323, bp352, bp373, bp415, bp416, bp423, bp444, bp458, bp469, bp470, bp484, bp489_2, bp499, bp500, bp503, bp508, bp523, bp527, bp528, bp536, bp543, bp553, bp328, bp398, bp437, bp439, bp451, bp457, bp480, bp489_1, bp492, bp507, bp509, bp514, bp531, bp534, bp551"
global vab "vp323, vp352, vp373, vp415, vp416, vp423, vp444, vp458, vp469, vp470, vp484, vp489_2, vp499, vp500, vp503, vp508, vp523, vp527, vp528, vp536, vp543, vp553, vp328, vp398, vp437, vp439, vp451, vp457, vp480, vp489_1, vp492, vp507, vp509, vp514, vp531, vp534, vp551"

preserve

foreach x in $abno {
cap drop p`x'
egen totyes`x' = total(yes`x') 
egen totvalid`x' = total (valid`x')
g p`x' =  (totyes`x'/totvalid`x')*100
}

foreach x in $ab {
reg `x' 
est store `x'
local name="`x'"
mat b`x'=_b[_cons]
mat v`x'= _se[_cons]^2
matrix coleq b`x' = `name'
matrix coleq v`x' = `name'
}
mat b= $bab
mat v= $vab
mat V=diag(v)
eret post b V
outreg2 using tables/T2, replace nose noaster bdec(1) onecol nor2 noobs ctitle("All cantons", "All mun.")

restore

/* German-speaking */
preserve
keep if french == 0

foreach x in $abno {
cap drop totyes`x'
cap drop totvalid`x'
cap drop p`x'
egen totyes`x' = total(yes`x') 
egen totvalid`x' = total (valid`x')
g p`x' =  (totyes`x'/totvalid`x')*100
}


foreach x in $abno {
cap drop totyes`x'
cap drop totvalid`x'
cap drop p`x'
egen totyes`x' = total(yes`x') 
egen totvalid`x' = total (valid`x')
g p`x' =  (totyes`x'/totvalid`x')*100
}

foreach x in $ab {
reg `x' 
est store `x'
local name="`x'"
mat b`x'=_b[_cons]
mat v`x'= _se[_cons]^2
matrix coleq b`x' = `name'
matrix coleq v`x' = `name'
}
mat b= $bab
mat v= $vab
mat V=diag(v)
eret post b V
outreg2 using tables/T2, nose noaster bdec(1) onecol nor2 noobs ctitle("All cantons", "German mun.")

restore

/* French-speaking */
preserve
keep if french == 1

drop if canton == "TI"
drop if french == 1 & canton == "GR"


foreach x in $abno {
cap drop totyes`x'
cap drop totvalid`x'
cap drop p`x'
egen totyes`x' = total(yes`x') 
egen totvalid`x' = total (valid`x')
g p`x' =  (totyes`x'/totvalid`x')*100
}


foreach x in $abno {
cap drop totyes`x'
cap drop totvalid`x'
cap drop p`x'
egen totyes`x' = total(yes`x') 
egen totvalid`x' = total (valid`x')
g p`x' =  (totyes`x'/totvalid`x')*100
}

foreach x in $ab {
reg `x' 
est store `x'
local name="`x'"
mat b`x'=_b[_cons]
mat v`x'= _se[_cons]^2
matrix coleq b`x' = `name'
matrix coleq v`x' = `name'
}
mat b= $bab
mat v= $vab
mat V=diag(v)
eret post b V
outreg2 using tables/T2, nose noaster bdec(1) onecol nor2 noobs ctitle("All cantons", "French mun.")

restore



/* Bilingual cantons: German-speaking */
preserve
keep if canton == "BE"| canton=="FR" | canton =="VS"
keep if french == 0

foreach x in $abno {
cap drop totyes`x'
cap drop totvalid`x'
cap drop p`x'
egen totyes`x' = total(yes`x') 
egen totvalid`x' = total (valid`x')
g p`x' =  (totyes`x'/totvalid`x')*100
}


foreach x in $abno {
cap drop totyes`x'
cap drop totvalid`x'
cap drop p`x'
egen totyes`x' = total(yes`x') 
egen totvalid`x' = total (valid`x')
g p`x' =  (totyes`x'/totvalid`x')*100
}

foreach x in $ab {
reg `x' 
est store `x'
local name="`x'"
mat b`x'=_b[_cons]
mat v`x'= _se[_cons]^2
matrix coleq b`x' = `name'
matrix coleq v`x' = `name'
}
mat b= $bab
mat v= $vab
mat V=diag(v)
eret post b V
outreg2 using tables/T2, nose noaster bdec(1) onecol nor2 noobs ctitle("Bilingual cantons", "German mun.")

restore



/* Bilingual cantons: French-speaking */
preserve
keep if canton == "BE"| canton=="FR" | canton =="VS"
keep if french == 1

foreach x in $abno {
cap drop totyes`x'
cap drop totvalid`x'
cap drop p`x'
egen totyes`x' = total(yes`x') 
egen totvalid`x' = total (valid`x')
g p`x' =  (totyes`x'/totvalid`x')*100
}


foreach x in $abno {
cap drop totyes`x'
cap drop totvalid`x'
cap drop p`x'
egen totyes`x' = total(yes`x') 
egen totvalid`x' = total (valid`x')
g p`x' =  (totyes`x'/totvalid`x')*100
}

foreach x in $ab {
reg `x' 
est store `x'
local name="`x'"
mat b`x'=_b[_cons]
mat v`x'= _se[_cons]^2
matrix coleq b`x' = `name'
matrix coleq v`x' = `name'
}
mat b= $bab
mat v= $vab
mat V=diag(v)
eret post b V
outreg2 using tables/T2, nose noaster bdec(1) onecol nor2 noobs ctitle("Bilingual cantons", "French mun.")

restore
